PHP File Handling
| scandir(‘path’) | Will scan the files in the requesting directory. Will only do the first level. |
|---|---|
| is_dir(“path”) | Will return a boolean on whether or not a path is a directory. |
| fopen(‘file’, ‘mode’) | A function that opens a connection to a file and returns a file handler. |
| fclose($file_handler) | Will chose the file handler and close the file. |
| feof($file_handler) | Returns of a boolean of whether or not you are at the end of the file. |
| fgets($file_handler) | Will get the line and move the pointer to the next line. |
| strpos($line, $regex) | Will check if a string contains the regex line (Like .contains() in java). |
| str_replace(‘pattern’, ‘replaced_pattern’, $string) | Will replace a pattern in a string with a different pattern. |
| file_put_contents(‘path_to_file’) | Will get the entire contents of the file and store it as a string. |
| file(‘path_to_file’) | Will store the contents of a file as a string. |
| fwrite($file_handler, ‘string’) | This function will write a string to a file. |
| strcasecmp($a, $b) | A capitalized and binary safe way to compare strings. |
| strip_tags($str) | Will strip any HTML tags off a string. |
| usort($array, ‘function_name’) | Will sort an array based on the function fed into it. |
| file_put_contents() | Will write an array to a file using the write mode or the append mode. |
| fgetcsv($fh) | Will return a line of csv as an array |
| array_flip($array) | Will flip an array such that the key is the value and the value is the key. |
| extract($array) | Will pull out the keys of an array into its own variable. |
| fputcsv($fh, $array) | Puts a new line in csv format to the end of a file. |
| json_decode($str) | Turns a string of JSON into a PHP object. |
| is_object() | Will check if a variable is an object. |
| json_encode($books, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
| opendir($dir) | Will open the entire directory as a file holder. |
| readdir($fh) | Will read the name of each file in the open directory. |
| $xml->addChild(‘item’, opt ‘value’) | Add items to an XML file. |
| $xml->asXML($file) | Load the XML data into the file. |
Directory and File Structures:
- Everything on a computer is stored on some sort of file.
- Opening and processing a file is common for application:
- Opening and interpreting a config file.
- Exporting data/order details.
- Permissions control who has access to files. If a file cannot be opened, be sure to view its permissions.
- Recall:
- . is the current directory
- .. is the parent directory
- Any file that starts with a dot is a hidden file.
- is_dir(‘path’) will return a boolean on whether or not a path is a directory.
Including and Opening files:
- When working with HTML files, you can use the include tag to add HTML.
- Ex: <?php include 'data/html/countries.html'; ?>
- fopen(‘path’, ‘mode’) is a function that opens a connection to a file and returns a file handler.
- It is good to include this function in an if statement in case the file load fails.
- fclose($file_handler) is a function that will close the file handler and the file.
- feof($fh) will return a boolean of whether or not you are at the end of a file.
- fgets($fh) will grad the line and move the pointer to the next line.
- str_replace() is a function that will replace a section of a string with another piece.
Reading Files Into a String or Array
- file_put_contents(“path”) will get the contents of the entire file and store it as a string.
- file(‘path’) will store the contents of the file as an array.
- Three Ways to Interact with a File:
- Open a connection to the file and handle it line-by-line before closing it
- Work with the entire file as a string
- Work with the entire file as an object or array.
Writing Files Line by Line:
- Writing files allows you to change files to update what is going one.
- You can open the file for writing by using the ‘w’ mode:
- fopen(‘file_name’, ‘w’);
- This will erase the contents, so be careful!
- fwrite($file_handler, ‘string’)
- PHP_EOL.’string’ will add the proper end of line character when writing a file.
Writing Files All at Once:
- Adding FILE_IGNORE_NEW_LINES to the file() function will make it ignore all the newline characters \n.
- strcasecmp() is a binary safe string comparison function.
- strip_tags() will strip the HTML tags off of a string.
- usort($array, ‘function_name’) is a function that will sort an array based on the function fed into it.
- file_put_contents(‘file_path’, ‘string’) will write the contents of a string to a file.
Personal Recommendations Project:
- Most popular file types:
- CSV
- JSON
- XML
Reading CSV:
- CSV stands for comma separated values
- Normally contains a header, and then the values below it.
- fgetcsv() will return an array of columns but only the line it is on.
- The first line will be the headers!
- Be sure to store the headers so you can use them when working with the rest of the file.
Writing CSV:
- Start by making an array of the values you want to add.
- Use the fputcsv($fh, $array) function to add a csv line to a file.
- Be sure to ensure that there is a newline before adding to a CSV.
- fseek($fh, offset, whence) can change where the pointer is located when viewing a file.
Reading JSON:
- JSON is an easy way to encode structured data. It is fairly easy for humans to read it.
- JSON - Javascript Object Notation.
- JSON can be converted into Objects or arrays.
- json_decode() turns a string into a PHP object.
- is_object() will check if a variable is an object.
- You can access the object by using ->.
Writing JSON:
- json_encode can be used to turn a PHP object back into JSON.
Reading XML:
- XML is a lot more detailed than JSON.
- Used for rss feeds for blogs or podcasts.
- When displaying XML, you need to include htmlspecialchars() so that the tags show up.
- To open a whole directory, you can use the opendir() command.
- readdir($fh) will read the file name of an open directory.
- simplexml_load_file($file) will load the XML file into a PHP object.
- This object can be accessed using the object syntax for PHP.
Writing XML:
- To add data to an xml file, you use the addChild() function
- The function takes two arguments
- Key
- Value
- The function takes two arguments
- XML does allow namespacing